package nju.bishe.weibo.data;

import nju.bishe.weibo.util.WeiboLocationManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import weibo4android.WeiboException;
import weibo4android.org.json.JSONException;
import weibo4android.org.json.JSONObject;

//地理位置点
public class Position implements Parcelable{
	//positon id
	private String pid;
	
	//name
	private String name;
	
	//address
	private String address;
	
	//类别
	private String category;
	
	//navigator
	private String navigator;
	
	//电话
	private String telephone;
	
	//longitude
	private String longitude;
	
	//latitude
	private String latitude;
	
	//city
	private String city;
	
	//province
	private String province;
	
	public Position(String longitude,String latitude){
		this.longitude = longitude;
		this.latitude = latitude;
	}


	//无参构造函数
	public Position(){
		
	}
	
	//从json对象构造
	public static Position constructFromJson(JSONObject jsonObject){
		Position position = new Position();
		try {
			position.name = jsonObject.getString("name");
			position.address = jsonObject.getString("address");
			position.category = jsonObject.getString("category");
			position.navigator = jsonObject.getString("navigator");
			position.telephone = jsonObject.getString("telephone");
			position.longitude = jsonObject.getString("longitude");
			position.latitude = jsonObject.getString("latitude");
			position.city = jsonObject.getString("city");
			position.province = jsonObject.getString("province");
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("wjc Location", "Postion contruct json object has error!");
		}
		return position;
	}
	
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getNavigator() {
		return navigator;
	}

	public void setNavigator(String navigator) {
		this.navigator = navigator;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}


	public String getProvince() {
		return province;
	}


	public void setProvince(String province) {
		this.province = province;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(pid);
		dest.writeString(name);
		dest.writeString(address);
		dest.writeString(category);
		dest.writeString(navigator);
		dest.writeString(telephone);
		dest.writeString(longitude);
		dest.writeString(latitude);
		dest.writeString(city);
		dest.writeString(province);
	}	
	

	public static final Parcelable.Creator<Position> CREATOR = new Parcelable.Creator<Position>() {
		public Position createFromParcel(Parcel in) {
			return new Position(in);
		}

		public Position[] newArray(int size) {
			return new Position[size];
		}
	};

	//
	private Position(Parcel in) {
		this.pid = in.readString();
		this.name = in.readString();
		this.address = in.readString();
		this.category = in.readString();
		this.navigator = in.readString();
		this.telephone = in.readString();
		this.longitude = in.readString();
		this.latitude = in.readString();
		this.city = in.readString();
		this.province = in.readString();
	}
	
	/**
	 * 将Location转为Position
	 * @param location
	 * @return
	 */
	public static Position constructFromLocation(Location location){
		WeiboLocationManager manager = new WeiboLocationManager();
		Position position = new Position(location.getLongitude()+"", location.getLatitude()+"");
		Address address = new Address();
		try {
			address = manager.geoToPosition(location.getLongitude(),location.getLatitude());
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		position.setCity(address.getCity());
		return position;
	}
}
